package com.yiran.dao;

import java.sql.*;
import java.util.*;

import com.yiran.model.StyleClass;
import com.yiran.util.DBConnection;

public class StyleBean {
	private Connection con;
	StyleClass style = new StyleClass();

	public StyleBean() {
		try {
			con = DBConnection.getConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void setStyleInfo(StyleClass style) {
		this.style = style;
	}

	public void addStyleInfo() throws Exception {
		try {

			PreparedStatement stm = con
					.prepareStatement("insert into produce_style(name) values(?)");
			stm.setString(1, style.getName());

			try {
				stm.executeUpdate();
			} catch (Exception e) {
			}

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	public Collection getStyles() {
		StyleClass style;
		ArrayList arraylist = new ArrayList();
		try {
			Statement statement = con.createStatement();
			ResultSet resultset = statement.executeQuery("select * from produce_style");

			while (resultset.next()) {
				style = new StyleClass();
				int id = resultset.getInt(1);
				String name = resultset.getString(2);

				style.setId(id);
				style.setName(name);
				arraylist.add(style);
			}
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return arraylist;
	}
	
	public StyleClass getStyle(String sid){
		StyleClass style;
		try {
			Statement statement = con.createStatement();
			ResultSet resultset = statement.executeQuery("select * from produce_style where id='"+sid+"'");

			while (resultset.next()) {
				style = new StyleClass();
				int id = resultset.getInt(1);
				String name = resultset.getString(2);

				style.setId(id);
				style.setName(name);
				return style;
			}
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public void deleteStyle(String s) {
		try {
			int i = Integer.parseInt(s);
			Statement statement = con.createStatement();
			statement.execute("delete from produce_style where id='" + i + "'");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
